Hello,
Could someone please elightenme on waht i’m doing wrong here ? Basically I’m trying to do a simple subtraction but the newly created sensor is remaining un available.
solar_consumption:
unit_of_measurement: "W"
entity_id:
- sensor.solar_power
- sensor.solar_battery_charging
- sensor.grid_feed_in
value_template: >-
{% set solarpower = states.sensor.solar_power.state|float %}
{% set batterycharge = states.sensor.solar_battery_charging.state| float %}
{% set feedin = states.sensor.feed_in.state| float %}
{{ solarpower - batterycharge - feedin }}
tom_l
July 10, 2021, 12:59pm
2
You are using the legacy template format. It must be formatted like this:
sensor:
- platform: template
sensors:
solar_consumption:
friendly_name: "Solar Consumption"
unit_of_measurement: "W"
value_template: >
{% set solarpower = states('sensor.solar_power')|float %}
{% set batterycharge = states('sensor.solar_battery_charging')|float %}
{% set feedin = states('sensor.feed_in')|float %}
{{ solarpower - batterycharge - feedin }}
Drop the sensor:
in the first line if you split your config and put sensors into a sensors.yaml file.
The entity_id
option has been depreciated. So I removed it.
I also changed your templates to use the states()
method to prevent errors when the state is unavailable. See the warning here .
If you want to use the new (and recommended) format:
template:
- sensor:
- name: "Solar Consumption"
unit_of_measurement: "W"
state: >
{% set solarpower = states('sensor.solar_power')|float %}
{% set batterycharge = states('sensor.solar_battery_charging')|float %}
{% set feedin = states('sensor.feed_in')|float %}
{{ solarpower - batterycharge - feedin }}
123
(Taras)
July 10, 2021, 12:59pm
3
If you paste the template portion into Developer Tools > Template, what does the Template Editor report as the result?
{% set solarpower = states.sensor.solar_power.state|float %}
{% set batterycharge = states.sensor.solar_battery_charging.state| float %}
{% set feedin = states.sensor.feed_in.state| float %}
{{ solarpower - batterycharge - feedin }}
For future reference, the entity_id
option for Template Sensors was deprecated many versions ago. It is no longer used by Home Assistant so you can remove that portion from the Template Sensor’s configuration.
1 Like
Thanks for your reply. Its the first time i’m trying to play around with calculations and the examples that i found seem to be related to the old method. I still didn’t put the senors in the sensor.yaml file and i place it in the conifg.yaml to test. For some reason i’m getting a popup with the following message. [object Object] but when I test the code in the developer tools it works. Any idea ?
{% set solarpower = states('sensor.solar_power')|float %}
{% set batterycharge = states('sensor.solar_battery_charging')|float %}
{% set feedin = states('sensor.feed_in')|float %}
{{ (solarpower - batterycharge) - feedin }}
tom_l
July 10, 2021, 1:51pm
5
Please list your full sensor config, not just the template.
sensor:
- platform: time_date
display_options:
- 'time'
- 'date'
- 'date_time'
- 'date_time_utc'
- 'date_time_iso'
- 'time_date'
- 'time_utc'
- 'beat'
- platform: plex_recently_added
token: XpjLXGcnNxgbwxYB8hH2
host: 192.168.1.105
port: 32400
- platform: integration
source: sensor.livingroom_plug_current_consumption
name: living_room_energy_spent
unit_prefix: k
round: 2
- platform: integration
source: sensor.fridge_plug_current_consumption
name: fridge_energy_spent
unit_prefix: k
round: 2
- platform: integration
source: sensor.gyzer_plug_current_consumption
name: gyzer_energy_spent
unit_prefix: k
round: 2
- platform: min_max
name: Brooder Mean Temperature
type: mean
entity_ids:
- sensor.brooder_lower_part_temperature
- sensor.brooder_higher_temp
- platform: template
sensors:
total_daily_energy:
friendly_name: Total Daily Energy
entity_id: sensor.time
value_template: >
{% set ns = namespace(states=[]) %}
{% for s in states.sensor %}
{% if s.object_id.startswith('daily_') and s.object_id.endswith('_energy') %}
{% set ns.states = ns.states + [ s.state | float ] %}
{% endif %}
{% endfor %}
{{ ns.states | sum }}
storage_discharge:
friendly_name: "Battery Discharge"
unit_of_measurement: "W"
value_template: >-
{% set u = state_attr('sensor.sun2000_3_68ktl_l1', 'storage_charge_discharge_power') | int %}
{% if u < 0 %}
{{ (-1*u) }}
{% else %}
{{ (0) | int }}
{% endif %}`
solar_power:
friendly_name: 'Solar power production'
unit_of_measurement: 'W'
value_template: >-
{{ state_attr("sensor.sun2000_3_68ktl_l1", "input_power") }}
battery_charge:
friendly_name: "Solar Battery Charge"
value_template: >-
{{ state_attr("sensor.sun2000_3_68ktl_l1", "storage_soc") }}
grid_feed_in:
unit_of_measurement: 'W'
value_template: >-
{{ state_attr("sensor.sun2000_3_68ktl_l1", "power_meter_active_power") | float | max(0) | round(1) }}
grid_consumption:
unit_of_measurement: "W"
value_template: >-
{{ state_attr("sensor.sun2000_3_68ktl_l1", "power_meter_active_power") | float | min(0) | abs | round(1) }}
power_meter:
unit_of_measurement: "W"
value_template: >-
{{ state_attr("sensor.sun2000_3_68ktl_l1", "power_meter_active_power") | float | abs | round(1) }}
solar_battery_charging:
unit_of_measurement: "W"
value_template: >-
{{ state_attr("sensor.sun2000_3_68ktl_l1", "storage_charge_discharge_power") | float | max(0) | abs | round(1) }}
solar_battery_consumption:
unit_of_measurement: "W"
value_template: >-
{{ state_attr("sensor.sun2000_3_68ktl_l1", "storage_charge_discharge_power") | float | min(0) | abs | round(1) }}
# solar_consumption:
# unit_of_measurement: "W"
# value_template: >
# {% set solar_generation = states('sensor.solar_power') | float %}
# {% set battery_charging = states('sensor.solar_battery_charging') | float %}
# {% set feed_in = states('sensor.grid_feed_in') | float %}
# {% set value = ( (solar_generation - battery_charging) - sensor.grid_feed_in ) | round(1) %}
# {% set value = ( 2 - 1 ) | round(1) %}
# solar_consumption:
# unit_of_measurement: "W"
# entity_id:
# - sensor.solar_power
# - sensor.solar_battery_charging
# - sensor.grid_feed_in
# value_template: >-
# {% set solarpower = states.sensor.solar_power.state|float %}
# {% set batterycharge = states.sensor.solar_battery_charging.state| float %}
# {% set feedin = states.sensor.feed_in.state| float %}
# {{ solarpower - feedin }}
solar_consumption:
unit_of_measurement: "W"
value_template: >
{% set solarpower = states('sensor.solar_power') | float %}
{% set batterycharge = states('sensor.solar_battery_charging') | float %}
{% set feedin = states('sensor.grid_feed_in') | float %}
{{ (solarpower - batterycharge) - feedin }}
# - platform: huawei_solar
# host: '192.168.1.204'
- platform: huawei_solar
host: '192.168.1.204'
optimizers: true
battery: true
- platform: command_line
name: Power
command: " cat /sys/class/power_supply/ACAD/device/power_supply/ACAD/online"
scan_interval: 1
value_template: '{% if value is equalto "1" %} OK {% else %} Power Outage {% endif %}'
- platform: linux_battery
battery: 1
- platform: dnsip
hostname: google.com
name: google_80.93.156.60
resolver: 80.93.156.60
scan_interval: 30
- platform: dnsip
hostname: google.com
name: google_80.93.157.60
resolver: 80.93.157.60
scan_interval: 30
- platform: dnsip
hostname: google.com
name: google_80.93.156.240
resolver: 80.93.156.240
scan_interval: 30
- platform: dnsip
hostname: google.com
name: google_80.93.157.240
resolver: 80.93.157.240
scan_interval: 30
- platform: dnsip
hostname: go.com.mt
name: go.com.mt_80.93.156.60
resolver: 80.93.156.60
scan_interval: 30
- platform: dnsip
hostname: go.com.mt
name: go.com.mt_80.93.157.60
resolver: 80.93.157.60
scan_interval: 30
- platform: dnsip
hostname: router10.teamviewer.com
name: rtr10teamviewer_80.93.156.60
resolver: 80.93.156.60
scan_interval: 30
- platform: template
sensors:
livingroom_plug_voltage:
value_template: >-
{{ states.switch.livingroomtvplug.attributes.voltage }}
unit_of_measurement: 'V'
livingroom_plug_current:
value_template: >-
{{ states.switch.livingroomtvplug.attributes.current }}
unit_of_measurement: 'mA'
livingroom_plug_current_consumption:
value_template: >-
{{ states.switch.livingroomtvplug.attributes.current_consumption }}
unit_of_measurement: 'W'
- platform: template
sensors:
gyzer_plug_voltage:
value_template: >-
{{ states.switch.gyzer_plug.attributes.voltage }}
unit_of_measurement: 'V'
gyzer_plug_current:
value_template: >-
{{ states.switch.gyzer_plug.attributes.current }}
unit_of_measurement: 'mA'
gyzer_plug_current_consumption:
value_template: >-
{{ states.switch.gyzer_plug.attributes.current_consumption }}
unit_of_measurement: 'W'
- platform: template
sensors:
fridge_plug_voltage:
value_template: >-
{{ states.switch.fridge_plug.attributes.voltage }}
unit_of_measurement: 'V'
fridge_plug_current:
value_template: >-
{{ states.switch.fridge_plug.attributes.current }}
unit_of_measurement: 'mA'
fridge_plug_current_consumption:
value_template: >-
{{ states.switch.fridge_plug.attributes.current_consumption }}
unit_of_measurement: 'W'
- platform: template
sensors:
battery_phone:
friendly_name: Etienne Phone Battery
unit_of_measurement: '%'
value_template: >-
{%- if state_attr('device_tracker.oneplus_a5000', 'battery_level') %}
{{ state_attr('device_tracker.oneplus_a5000', 'battery_level')|round }}
{% else %}
{{ states('device_tracker.xxxxx') }}
{%- endif %}
device_class: battery
- platform: template
sensors:
battery_phone:
friendly_name: Ramona Phone Battery
unit_of_measurement: '%'
value_template: >-
{%- if state_attr('device_tracker.redmi_k20_pro', 'battery_level') %}
{{ state_attr('device_tracker.redmi_k20_pro', 'battery_level')|round }}
{% else %}
{{ states('device_tracker.xxxxx') }}
{%- endif %}
device_class: battery
power_status:
entity_id: sensor.power
value_template: "{%- if is_state('sensor.power', '1') -%} OK {%- else -%} Power Outage {%- endif %}"`
tom_l
July 10, 2021, 2:38pm
7
Please format it correctly.
tom_l
July 10, 2021, 3:00pm
9
This
solar_consumption:
unit_of_measurement: "W"
value_template: >
{% set solarpower = states('sensor.solar_power') | float %}
{% set batterycharge = states('sensor.solar_battery_charging') | float %}
{% set feedin = states('sensor.grid_feed_in') | float %}
{{ (solarpower - batterycharge) - feedin }}
Is not indented far enough.
It should be in line with the sensors above
That did the trick. Usually re indentation the file editor would give a red ! with error as bad indentation.
What is the best editor I could use please ?
tom_l
July 10, 2021, 3:15pm
11
The one I’m using above is VSCode with the Home Assistant Helper, rainbow indentation and rainbow brackets extensions. I use the Samba addon to to share the /config folder on my local network so I can run this on my PC.
However there is also an Addon for VSCode if you want to access it via Home Assistant. It comes with the home assistant helper extension pre-installed.
123
(Taras)
July 10, 2021, 5:21pm
12
There is more than meets the eye here.
This topic’s solution is to correct the indenting of the sensor’s configuration. However, Home Assistant will not create a Template Sensor that has improper indentation. Yet the claim is that it is “newly created”.
I added the Template Sensor’s configuration to my system and ensured it had incorrect indentation. The result of running Check Configuration is:
I ignored the error message and Reloaded Template Entities in an attempt to force Home Assistant to create the incorrectly indented sensor. The result is this message in Logs:
Invalid config for [sensor.template]: [solar_consumption] is an invalid option for [sensor.template]. Check: sensor.template->solar_consumption. (See /config/sensors/sensors.yaml, line 152). Please check the docs at Template - Home Assistant
Most importantly, the sensor was not created.
@Etienne_Vella
Did you ever have a functional sensor.solar_consumption
entity and then you attempted to modify it? n other words, it was created using a correct configuration and then you modified it and introduced incorrect indenting?
Because Home Assistant (at least not a recent version) will not create a Template Sensor with an improperly indented configuration.