Automation to change the value of an attribute of an arbitrary (switch or sensor) entity

I am looking for a Homeassistant automation of an action to copy the value of attributes of an (arbitrary) existing entity into another entity. Something like:
switch.homewizard.attributes.state = sensor.homewizard.attributes.state. So, an exchange between the homewizard sensor platform and the homewizard switch platfrom
I could not find a standard service call for this process. I am not yet very experienced with Homeassistant, so I would appreciate any guidance.

Background:
In my Homeassistant configuration I have created 2 platforms to interface with the Homewizard box which I use here as a hub: one platform for the sensors and a second for the switches. Both get their update data from identical request.get calls to the Homewizard box. This setup allows me to control about 42 switches and 17 sensors (including thermometers, hygrometers and alarm devices). All is working reasonably well in Homeassistant, apart from occasional timeouts in the scanning process which I had to timeout.
In the setup and updating process of Homeassistant each platform gets all Homewizard data using request.get(url, timeout=…), which means that the same information is always asked for twice (only twice, because within each platform I keep these data for about 10 sec so that all 42 switches - or 17 sensors - can be updated with the same data). This is done in each platform using an intermediate object HomewizardData which contains all information that was retrieved from the initial and update calls to the Homewizard. In order to make the content of these 2 objects available as Homeassistant entities I have created the sensor.homewizard and the switch.homewizard.
Now I’d like to reduce the load on the network and the Homewizard box by avoiding the request.get call for the switches. This could be done by copying the data from the sensor.homewizard entity and loading it into the switch.homewizard entity. This entity makes the data then available at the lower level to the intermediary HomewizardData object from which the 42 switches derive their content.
Then only one request.get call is needed for per update scan.

I would think you’d want to do this within the implementation of the sensor & switch, rather than doing it at the automation level. E.g., whichever one (sensor or switch) does the requests.get, have it save the data in hass.data['homewizard'], and then the other can retrieve the data from the same data structure.

Thank you, pnbruckner. What you suggest would indeed be my preferred solution. I was not aware that I could play with the hass object, the doc ‘dev_101_hass’ did not give me that impression. Do I understand you well that I can set ‘data’ as an attribute to the ‘hass’ object? Or can I simply append to an existing hass.data? Do I do this in configuration.yaml?

hass.data is a dictionary that your component/platform code can use to save and retrieve component/platform specific data. You need to use a unique key (e.g., as I showed above.) You write and read this data structure from your component/platform code. You should browse around existing code to see how it is used.

Thank you pnbruckner. This clarifies a lot, you have helped me a lot with your responses. I am going to rework my platforms in this way.
Regards,
Martijn Höfelt

1 Like