rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.76k stars 535 forks source link

Energy monitoring using config suggested in Readme.md doesn't work #1148

Open spitfire opened 1 year ago

spitfire commented 1 year ago

I'm trying to set up Aubess Smart Socket 20A/EM (entity name is switch.aubess_smart_socket_20a_em_serwerownia) for energy monitoring. I have the following config in configuration.yaml for the - platform: template section:

- platform: template
    sensors:
      energia_czynna_aktualnie_pobierana:
        friendly_name: "Energia czynna aktualnie pobierana"
        device_class: energy
        value_template: >-
          {{ (states('sensor.electricity_meter_power_active_phase_1')|float  + states('sensor.electricity_meter_power_active_phase_2')|float + states('sensor.electricity_meter_power_active_phase_3')|float) | round(0) }}
        unit_of_measurement: "W"
      tuya-sw01_voltage:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.voltage }}
        unit_of_measurement: 'V'
      tuya-sw01_current:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.current }}
        unit_of_measurement: 'mA'
      tuya-sw01_current_consumption:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.current_consumption }}
        unit_of_measurement: 'W'

and when I try to Check configuration in Developer tools I get the following error:

Configuration invalid!

Invalid config for [sensor.template]: invalid slug tuya-sw01_voltage (try tuya_sw01_voltage) for dictionary value @ data['sensors']. Got OrderedDict([('energia_czynna_aktualnie_pobierana', OrderedDict([('friendly_name', 'Energia czynna aktualnie pobierana'), ('device_class', 'energy'), ('value_template', "{{ (states('sensor.electricity_meter_power_active_phase_1')|float + states('sensor.electricity_meter_power_active_phase_2')|float + states('sensor.electricity_meter_power_active_phase_3')|float) | round(0) }}"), ('unit_of_measurement', 'W')])), ('tuya-sw01_voltage', OrderedDict([('value_template', '{{ states.switch.aubess_sm.... (See ?, line ?).

tbgoose commented 1 year ago

You were missing a few brackets in your multi sensor. Try below replacing your sensors.

{{ ((states('sensor.kitchen_socket_local_power') | float ) + (states('sensor.kitchen_fridge_socket_local_power') | float ) + (states('sensor.living_room_socket_local_power') | float )) | round(0) }}

spitfire commented 1 year ago

You were missing a few brackets in your multi sensor. Try below replacing your sensors.

{{ ((states('sensor.kitchen_socket_local_power') | float ) + (states('sensor.kitchen_fridge_socket_local_power') | float ) + (states('sensor.living_room_socket_local_power') | float )) | round(0) }}

Are you sure this is relevant to my issues? I'm trying to get 3 parameters out of the Tuya device, the first template is working just fine. When I add the entries for Tuya, not so much anymore.

tbgoose commented 1 year ago

Nope not at all, I just noticed the yaml wasn't the way I do it, and when I copied yours into the template tool I got errors. Ignore that one if it is fine for you

I have to seperate my templates and repeat the - platform line each time, maybe try that?

- platform: template
    sensors:
      energia_czynna_aktualnie_pobierana:
        friendly_name: "Energia czynna aktualnie pobierana"
        device_class: energy
        value_template: >-
          {{ (states('sensor.electricity_meter_power_active_phase_1')|float  + states('sensor.electricity_meter_power_active_phase_2')|float + states('sensor.electricity_meter_power_active_phase_3')|float) | round(0) }}
        unit_of_measurement: "W"
- platform: template
    sensors:
      tuya-sw01_voltage:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.voltage }}
        unit_of_measurement: 'V'
- platform: template
    sensors:      
      tuya-sw01_current:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.current }}
        unit_of_measurement: 'mA'
- platform: template
    sensors:
      tuya-sw01_current_consumption:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.current_consumption }}
        unit_of_measurement: 'W'

To confirm, when you paste the following into the template tool it works?

- platform: template
    sensors:
      tuya-sw01_voltage:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.voltage }}
        unit_of_measurement: 'V'
spitfire commented 1 year ago

Thanks a lot for your reply! Tried this, and it still fails the "Check configuration" in "Developer tools"

  - platform: template
    sensors:
      energia_czynna_aktualnie_pobierana:
        friendly_name: "Energia czynna aktualnie pobierana"
        device_class: energy
        value_template: >-
          {{ (states('sensor.electricity_meter_power_active_phase_1')|float  + states('sensor.electricity_meter_power_active_phase_2')|float + states('sensor.electricity_meter_power_active_phase_3')|float) | round(0) }}
        unit_of_measurement: "W"

  - platform: template
    sensors:
      tuya-sw01_voltage:
        value_template: >-
          {{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.voltage }}
        unit_of_measurement: "V"

Invalid config for [sensor.template]: invalid slug tuya-sw01_voltage (try tuya_sw01_voltage) for dictionary value @ data['sensors']. Got OrderedDict([('tuya-sw01_voltage', OrderedDict([('value_template', '{{ states.switch.aubess_smart_socket_20a_em_serwerownia.attributes.voltage }}'), ('unit_of_measurement', 'V')]))]). (See ?, line ?).

attaching visual representation of the config, so that you can see the whitespace:

Zrzut ekranu 2022-12-8 o 11 27 39
dincojazz commented 1 year ago

Hi! Because you only use one value and not add several, I think you should replace "value_template:" with "state:" in the second part of the code:

spitfire commented 1 year ago

Does not work when added as is (GitHub breaks formatting, so that's why I'm sending screenshots) Zrzut ekranu 2022-12-8 o 14 41 29. When I added line break this happens Zrzut ekranu 2022-12-8 o 14 44 15 so it seems it needs to be value_template, not a state.

spitfire commented 1 year ago

I'm trying to set up Aubess Smart Socket 20A/EM (entity name is switch.aubess_smart_socket_20a_em_serwerownia)

corrected entity name - it is "switch.aubess_smart_socket_20a_em_serwerownia", not "aubess_smart_socket_20a_em_serwerownia.attributes" if that matters.

dincojazz commented 1 year ago

I'm trying to set up Aubess Smart Socket 20A/EM (entity name is switch.aubess_smart_socket_20a_em_serwerownia)

corrected entity name - it is "switch.aubess_smart_socket_20a_em_serwerownia", not "aubess_smart_socket_20a_em_serwerownia.attributes" if that matters.

So... Try this with both variants with "state:" and next time with "value_template:" with correct entity name, but with this formatting below - inside the curly braces; closed with braces; and closed between upper commas. That's what I can suggest... I'm sorry if it can't fix. I'm not so good in programming.

patrik-malina commented 1 year ago

Create instead 3 additional sensors with config.

spitfire commented 1 year ago

So... Try this with both variants with "state:" and next time with "value_template:" with correct entity name, but with this formatting below - inside the curly braces; closed with braces; and closed between upper commas. That's what I can suggest... I'm sorry if it can't fix. I'm not so good in programming.

Unfortunately that didn't work either. thanks for trying though

Create instead 3 additional sensors with config.

What do you mean? I think I understand the basics, but I'm pretty new to it.

tbgoose commented 1 year ago

These are absolutely correct. They work for me... Stop using the config and get it working under Developer Tools / Template first. Do one sensor at a time, and only add them to your config once you confirm they give you what you want.

- platform: template
    sensors:
      energia_czynna_aktualnie_pobierana:
        friendly_name: "Energia czynna aktualnie pobierana"
        device_class: energy
        value_template: >-
          {{ ((states('sensor.kitchen_socket_power') | float ) + (states('sensor.kitchen_fridge_socket_power') | float ) + (states('sensor.living_room_socket_power') | float )) | round(0) }}
        unit_of_measurement: "W"

- platform: template
    sensors:
      tuya-sw01_voltage:
      state: >-
          {{ states.sensor.gym_desk_socket_power.attributes.raw_state }}
      unit_of_measurement: "W"
Result type: string

- platform: template
    sensors:
      energia_czynna_aktualnie_pobierana:
        friendly_name: "Energia czynna aktualnie pobierana"
        device_class: energy
        value_template: >-
          167
        unit_of_measurement: "W"

- platform: template
    sensors:
      tuya-sw01_voltage:
      state: >-
          81.1
      unit_of_measurement: "W"

This template listens for the following state changed events:

    Entity: sensor.gym_desk_socket_power
    Entity: sensor.kitchen_fridge_socket_power
    Entity: sensor.kitchen_socket_power
    Entity: sensor.living_room_socket_power

We don't have your sensor names and IDs. You need to make sure the sensor id and the attribute id are correct for your sensors. If you don't know for certain look at the entity/device in the Developer Tools / States and confirm. I have just used some of mine that I know have W units or attributes to test the code. Once you get the code working all you need to change is the entity id, so just do it once and then replicate once working.

patrik-malina commented 1 year ago

So... Try this with both variants with "state:" and next time with "value_template:" with correct entity name, but with this formatting below - inside the curly braces; closed with braces; and closed between upper commas. That's what I can suggest... I'm sorry if it can't fix. I'm not so good in programming.

Unfortunately that didn't work either. thanks for trying though

Create instead 3 additional sensors with config.

What do you mean? I think I understand the basics, but I'm pretty new to it.

When configuring through gui, first set switch entity, then uncheck do nod add more entities, and add 3 more sensor type entity with corresponding IDs and class {current, power, voltage). I can later post screenshots if you wouldn't success.

spitfire commented 1 year ago

These are absolutely correct. They work for me... Stop using the config and get it working under Developer Tools / Template first. Do one sensor at a time, and only add them to your config once you confirm they give you what you want.

- platform: template
    sensors:
      energia_czynna_aktualnie_pobierana:
        friendly_name: "Energia czynna aktualnie pobierana"
        device_class: energy
        value_template: >-
          {{ ((states('sensor.kitchen_socket_power') | float ) + (states('sensor.kitchen_fridge_socket_power') | float ) + (states('sensor.living_room_socket_power') | float )) | round(0) }}
        unit_of_measurement: "W"

- platform: template
    sensors:
      tuya-sw01_voltage:
      state: >-
          {{ states.sensor.gym_desk_socket_power.attributes.raw_state }}
      unit_of_measurement: "W"
Result type: string

- platform: template
    sensors:
      energia_czynna_aktualnie_pobierana:
        friendly_name: "Energia czynna aktualnie pobierana"
        device_class: energy
        value_template: >-
          167
        unit_of_measurement: "W"

- platform: template
    sensors:
      tuya-sw01_voltage:
      state: >-
          81.1
      unit_of_measurement: "W"

This template listens for the following state changed events:

    Entity: sensor.gym_desk_socket_power
    Entity: sensor.kitchen_fridge_socket_power
    Entity: sensor.kitchen_socket_power
    Entity: sensor.living_room_socket_power

We don't have your sensor names and IDs. You need to make sure the sensor id and the attribute id are correct for your sensors. If you don't know for certain look at the entity/device in the Developer Tools / States and confirm. I have just used some of mine that I know have W units or attributes to test the code. Once you get the code working all you need to change is the entity id, so just do it once and then replicate once working.

Thanks for the info, didn't know about the template tool at all. The queries in the curly brackets do work (e.g. display right information):

Zrzut ekranu 2022-12-9 o 11 13 46

But when I try to paste the same into configuration.yaml is when I get errors. I had the first template sensor in config for a long time and it worked, but when I try to add the tuya ones:

Zrzut ekranu 2022-12-9 o 11 18 35

here's what happens in dev tools/yaml/check configuration:

Zrzut ekranu 2022-12-9 o 11 18 58
tbgoose commented 1 year ago

It says it doesn't like the - in the entity name. Replace tuya-sw01 with tuya_sw01

Message ID: @.***>

spitfire commented 1 year ago

It says it doesn't like the - in the entity name. Replace tuya-sw01 with tuya_sw01

ugh...thanks a lot! That was it. And that is exactly how it is written in Readme.md of this project - with a dash instead of underscore.

Sorry to bother you again, but based on the current consumption how can I get a meter that outputs the combined consumption in kWh from the current_consumption in Watts?

tbgoose commented 1 year ago

That's annoying isn't it!

You are looking to create a sum reimann integral sensor. Will look something like this

sensor:
  - platform: integration
    source: sensor.tuya_sw01_current_consumption
    name: tuya_sw01_energy
    unit_prefix: k
    round: 2
    method: left

Can also do this as a helper in GUI if you prefer. All the same settings are able to be selected.

patrik-malina commented 1 year ago

Sorry for late respond. Here you go screenshots for configuring energy sensors. Then in helpers create sum for kWh and finally in helpers create monthly energy. Hope this helps... Screenshot (1) Screenshot (2) Screenshot (3) Screenshot (4) Screenshot (5) Screenshot (6)

Screenshot (7) Screenshot (8) Screenshot (9)

gquach commented 1 year ago

@patrik-malina Thank you for the screenshots, they finally allowed me to get my Arlec Grid Connect smartplugs with energy monitoring working correctly after a dozen hours of messing around with no success. You're a lifesaver!