Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heating_thermostat_update.py mapping thermostat with sensor #5

Closed
SimonO93 opened this issue Nov 16, 2018 · 2 comments
Closed

heating_thermostat_update.py mapping thermostat with sensor #5

SimonO93 opened this issue Nov 16, 2018 · 2 comments

Comments

@SimonO93
Copy link

SimonO93 commented Nov 16, 2018

Thank you for awesome script, I just need to change it a little as my thermostats and sensors not exactly share name parts.
Here is my script version, where I added 3 optional arguments:

 - thermostats          - list of thermostats to match in pairs with sensors
 - sensors              - list of sensors to match in pairs with thermostats
 - entity_id            - entity id of trigger

I use it as this in automations:

- id: heating_update_thermostats
  alias: 'Heating Update Thermostats'
  trigger:
    platform: state
    entity_id:
      - sensor.motion_biuro_temperature
      - sensor.motion_chlopcy_temperature
      - sensor.motion_sypialnia_temperature
      - sensor.motion_salon_tv_temperature
      - sensor.motion_kuchnia_temperature
      - climate.heater_biuro_f
      - climate.heater_dzieci_d
      - climate.heater_sypialnia_d
      - climate.heater_salon_d
      - climate.heater_jadalnia_d
  condition:
    condition: template
    value_template: >-
      {% if 'heater' in trigger.entity_id and trigger.to_state.attributes.current_temperature == none %}
        true
      {% elif 'sensor' in trigger.entity_id %}
        true
      {% else %}
        false
      {% endif %}
  action:
    service: python_script.heating_thermostat_update
    data_template:
      sensors:
        - sensor.motion_biuro_temperature
        - sensor.motion_chlopcy_temperature
        - sensor.motion_sypialnia_temperature
        - sensor.motion_salon_tv_temperature
        - sensor.motion_kuchnia_temperature
      thermostats:
        - climate.heater_biuro_f
        - climate.heater_dzieci_d
        - climate.heater_sypialnia_d
        - climate.heater_salon_d
        - climate.heater_jadalnia_d
      entity_id: '{{trigger.entity_id}}'

Maybe you would like to share that version, I know more ppl asked for it.

"""
Update Z-Wave thermostats (e.g. Danfoss 014G0013) state and current temperature
from external sensor.
Arguments:
 - thermostat			- thermostat entity_id (required)
 - sensor				- sensor entity_id (required)
 - heat_state			- name of heating state, default 'heat' (optional),
                          changing heat_state from the default value will broke
                          compatibility with HomeKit
 - idle_state			- name of idle state, default 'off' (optional),
                          changing idle_state from the default value will broke
                          compatibility with HomeKit
 - idle_heat_temp		- temperature value between 'idle' and 'heat' states,
                          default 8 (optional)
 - thermostats          - list of thermostats to match in pairs with sensors
 - sensors              - list of sensors to match in pairs with thermostats
 - entity_id            - entity id of trigger

Configuration example:

service: python_script.heating_thermostat_update
data:
  thermostat: climate.thermostat_kitchen
  sensor: sensor.temperature_kitchen
  heat_stat: 'auto'
  idle_state: 'idle'
  idle_heat_temp: 10

or with mappings
service: python_script.heating_thermostat_update
data:
  thermostats:
    - climate.thermostat_kitchen
    - climate.thermostat_room
  sensors:
    - sensor.temperature_kitchen
    - sensor.room_temperature
  entity_id: sensor.room_temperature
  heat_stat: 'auto'
  idle_state: 'idle'
  idle_heat_temp: 10

Script supports custom_updater component. Add this to your configuration and
stay up-to-date.

custom_updater:
  track:
    - python_scripts
  python_script_urls:
    - https://raw.githubusercontent.com/bieniu/home-assistant-config/master/python_scripts/python_scripts.json
"""

VERSION = '0.2.6'

ATTR_THERMOSTAT = 'thermostat'
ATTR_SENSOR = 'sensor'
ATTR_HEAT = 'heat_state'
ATTR_IDLE = 'idle_state'
ATTR_IDLE_HEAT = 'idle_heat_temp'
ATTR_CURRENT_TEMP = 'current_temperature'
ATTR_OPERATION_LIST = 'operation_list'
ATTR_OPERATION_MODE = 'operation_mode'
ATTR_TEMPERATURE = 'temperature'
ATTR_THERMOSTATS = 'thermostats'
ATTR_SENSORS = 'sensors'
ATTR_ENTITY_ID = 'entity_id'

ATTR_HEAT_STATE = 'heat'
ATTR_IDLE_STATE = 'off'
ATTR_IDLE_HEAT_STATE = 8

thermostat_id = data.get(ATTR_THERMOSTAT)
sensor_id = data.get(ATTR_SENSOR)
entity_id = data.get(ATTR_ENTITY_ID)
thermostats = data.get(ATTR_THERMOSTATS)
sensors = data.get(ATTR_SENSORS)
heat_state = data.get(ATTR_HEAT, ATTR_HEAT_STATE)
idle_state = data.get(ATTR_IDLE, ATTR_IDLE_STATE)
idle_heat_temp = data.get(ATTR_IDLE_HEAT, ATTR_IDLE_HEAT_STATE)

logger.error("thermostats {}.".format(thermostats))
logger.error("sensors {}.".format(sensors))
logger.error("entity_id {}.".format(entity_id))

def getIndex(list, value):
    if value in list:
        return list.index(value)
    return -1

if entity_id:
    index = getIndex(thermostats, entity_id)
    if index>=0:
        thermostat_id = entity_id
        sensor_id = sensors[index]
    else:
        index = getIndex(sensors, entity_id)
        if index>=0:
            sensor_id = entity_id
            thermostat_id = thermostats[index]

logger.error("Values: {} - {}.".format(thermostat_id, sensor_id))

if thermostat_id and sensor_id:
    try:
        temp = float(hass.states.get(sensor_id).state)
    except ValueError:
        logger.error("Could not get state of {}.".format(sensor_id))
    thermostat = hass.states.get(thermostat_id)
    if thermostat is None:
        logger.error("Could not get state of {}.".format(thermostat_id))
    else:
        attributes = thermostat.attributes.copy()
        attributes[ATTR_CURRENT_TEMP] = temp
        attributes[ATTR_OPERATION_LIST] = [heat_state, idle_state]
        if float(attributes[ATTR_TEMPERATURE]) > idle_heat_temp:
            state = heat_state
            attributes[ATTR_OPERATION_MODE] = heat_state
        else:
            state = idle_state
            attributes[ATTR_OPERATION_MODE] = idle_state
        hass.states.set(thermostat_id, state, attributes)
else:
    logger.error("Expected {} and {} entity_id, got: {} and {}.".format(
        ATTR_THERMOSTAT, ATTR_SENSOR, thermostat_id, sensor_id))

@bieniu
Copy link
Owner

bieniu commented Nov 22, 2018

I have in my TO-DO list rewriting this script and adding new options to configure. The general assumption was that this script should be as simple as possible so I don't know if I will use your solution. If someone needs a more flexible solution then there is a version for AppDaemon.

@cbirkenbeul
Copy link

@SimonO93 I tried your script, but get a lot of error message. The script itself seems to work fine. You know something about it?

hass

@bieniu bieniu closed this as completed Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants