my problem was i could not import yaml because habapp always complained
Exception in load: No module named 'yaml'
but when i wanted to install pyyaml on command line i received:
Requirement already satisfied: pyyaml in /usr/lib/python3/dist-packages (5.3.1)
now i found a way (hopefully correct) how to install pyyaml for habapp from commandline:
cd /opt/habapp
source bin/activate
pip install pyyaml
now i can import yaml and created a rule to update values that where changed through an item, with the benefit from habapp that if i edit the yaml file direct the values are passed to the rule without need to reload:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# HABApp:
# reloads on:
# - params/heating_demo.yml
"""
example how to have a parameter file that automatically passes values to the rule
without need to reload the rule and additionally values can be changed from an item
and the new value will be updated in the yaml file
(the dictionary at the end of the file is only needed for the very first load to
create the yaml file)
"""
import logging
import yaml
from HABApp import Rule, DictParameter, Parameter
from HABApp.openhab.items import OpenhabItem
from HABApp.openhab.events import ItemStateChangedEventFilter
class Heating(Rule):
def __init__(self, key, values):
super().__init__()
self.log = logging.getLogger('My_HABApp')
self.log.debug('### Heating: rule (re)loaded ###')
para_file = 'heating_demo'
self.zone_parameter = DictParameter(para_file, key, default_value=values)
self.yaml_file = f'/etc/openhab/habapp/params/{para_file:s}.yml'
self.zone_name = key
# working this way the variable will NOT be updated when yaml changed:
# self.setpoint_item_name = self.zone_parameter['setpoint_item_name']
# working this way the variable will be updaten when yaml changed:
self.setpoint_item_name = Parameter(para_file, key, "setpoint_item_name").value
self.setpoint_temp = Parameter(para_file, key, "setpoint_temp").value
# set item with saved value from yaml file
self.oh.post_update(self.setpoint_item_name,
self.setpoint_temp)
# init trigger for regularly query of the temp sensors
self.run.every(0, 10, self.parameter_info)
# init trigger if item for setpoint temp was adjusted
OpenhabItem.get_item(self.setpoint_item_name).\
listen_event(self.setpoint_temp_changed, ItemStateChangedEventFilter())
def setpoint_temp_changed(self, event):
'''
setpoint temp changed, update value in yaml file
'''
self.log.debug('### Heating: setpoint_temp_changed from '\
f'{self.setpoint_temp} to {event.value} degrees ###')
config = yaml.safe_load(open(self.yaml_file, encoding="utf-8"))
config[self.zone_name]['setpoint_temp'] = float(event.value)
yaml.dump(config, open(self.yaml_file, 'w', encoding="utf-8"))
def parameter_info(self):
'''
see all values that are stored in the yaml file
'''
# self.log.debug(f'### this are the parameters to work with: {self.zone_parameter}')
self.log.debug(f'### this is the parameter self.setpoint_item_name: {self.setpoint_item_name}')
heating_zones_dict = {
'EK_Fbh_FlEWoK' : {
'sensor_item_names' : ['iEK_Fbh_FlEWoK_Sensor'], # Temperatur Sensor Item(s)
'sensor_owns' : ['*#4*1*0##'], # OWN(s) fĆ¼r Abfrage Temperatur(en)
'setpoint_item_name' : 'iEK_Fbh_FlEWoK_Set', # Item zur Einstellung Soll-Temperatur
'setpoint_temp' : 21.0, # (Grad) Soll-Temp (nur Erstinit.)
'actor_item_names' : ['iEK_Fbh_FlEWoK_Actor'], # Aktor fĆ¼r Fbh Stellantrieb
'state_item_name' : 'iEK_Fbh_FlEWoK_State'
},
'EK_Fbh_Schlafen' : {
'sensor_item_names' : ['iEK_Fbh_Schlafen_Sensor'],
'sensor_owns' : ['*#4*2*0##'],
'setpoint_item_name' : 'iEK_Fbh_Schlafen_Set',
'setpoint_temp' : 11.0,
'actor_item_names' : ['iEK_Fbh_Schlafen_Actor'],
'state_item_name' : 'iEK_Fbh_Schlafen_State'
},
'EK_Fbh_Bad' : {
'sensor_item_names' : ['iEK_Fbh_Bad_Sensor'],
'sensor_owns' : ['*#4*3*0##'],
'setpoint_item_name' : 'iEK_Fbh_Bad_Set',
'setpoint_temp' : 22.0,
'actor_item_names' : ['iEK_Fbh_Bad_Actor'],
'state_item_name' : 'iEK_Fbh_Bad_State'
}
}
for k, v in heating_zones_dict.items():
Heating(k, v)
of course, this could also be achieved with a persisted item, my goal was to have all parameters for heating together