# Newbie! - Need help with simple code

Hi guys - Newbie here. heeeeeeelp

I’ve got Sonoff switches all over the house, and have no problem doing everyday simple things with them via HA. However, I’d like to do something a little more technical, and don’t really know where to start with the code: it’s probably super-simple for y’all. Can you help me out please.

Idea:
Using 2 switches and a wifi electricity meter, I want to change the SOURCE of electricity to power an item.

Triggers: a) On the 4th day of every month b) when the value of a variable reaches 3 kilowatts

Variables:
Mactual = current reading on the Wifi electricity meter
Mcount = Variable to be reset to (0) zero at ‘change over’
Mdiff = difference between Mactual and Mcount

Code

at the 4th of each month:

If date = “4” then:
Mcount = Mactual (reset Mcount to actual meter reading as a baseline to start)
Switch A = ON
delay 1 second
Switch B = OFF

When the usage reaches 3 kw electricity

if Mdiff = 3.0 kilowatts then:
Switch B= ON
delay for 1 second
Switch A = OFF

** I know I haven’t given the power meter brand, etc… but what would be a “template” I can use.

Thanks !!

Use Automations and ‘Choose’ in actions? This allows some sort of if/then/else by using Options having their own conditions and actions

Thanks. Is there any way to do this via code. I have looked at the automation section… and (Even as an avionics mechanic on aircraft) I can’t, for the life of me figure it out 100%.

Yeah…you do have a point, I did not think about the date part… as I donot have the devices some diea of steps…not sure if I fully understand just trying to give ideas:

Create Automation and set
Trigger: time, choose some time
Condition: {{ now().strftime("%d") == “04” }}
Then in actions, choose
Option 1:
Condition device something state <3.0kw
Action: switchA on
Action delay = 1s
Action switchB off
Option 2:
Condition: device something state =3.0kw
etc.

OK! Figured that one out… thank you. Vingerha.

Now, I have one more hurdle… can you throw me another bone , please !

I have only one WIFI power meter installed which is the total power of the house.
I have another power source (Hydro) that feed the house as well.
Here’s what I need to accomplish.

Home Energy Tab in HA has many ‘souces’ - but as I said I only have 1 meter.
I’d like to create 2 .sensors under ‘integrations’ to use in the Energy Tab of HA.

however, when switch A is on… sensor 1 will show power from the meter and sensor 2 will say “0” - and when switch B is ‘on’ – sensor 2 will show power from the meter, and sensor 1 will say “0”

Can I assign variables this data, and then use the variable in the value of the sensor ??

I’m sure there’s an easier way.

Trying to understand (you do ask a bit after a days work and thinking out loudly (well…in writing)
From what I see at my place, the integration sensor are used by HA as statistics, they use the referred underlying sensor to agg per day. e.g. my manually created integration sensors refer to manually created template sensors which show the W values of my tuya power plugs. I am pretty sure this can be done more efficiently but I haven’t found out how yet’(and it works for me)
So…if I understand you well enough
template sensor 1 would show the W when switch A is on (else 0)
template sensor 2 would show the W when switch B is on (else 0)
then two additional sensors integration sensor 1 would aggregate on template sensor 1 and… 2 on 2.
Is this sort of the direction you are thinking of?

You could add something like this to your config (I am not going to explain how to add sensors in detail…lots of documentation around and a important learning moment…believe me )

``````  - platform: template
sensors:
power_consumption1:
friendly_name: "PowerCons1"
unit_of_measurement: 'W'
device_class: power
value_template: ->
"{% if states('sensor.switchA') == on %}
{{ states.yourpowersource.yourattributes.yourattribvalue }}
{% endif %}
"
- platform: integration
source: sensor.power_consumption1
name: powerconsumption1
method: left
round: 2
unit_prefix: k
``````

If all works, the first one should show a value in the state indicating power when A is on
The integration sensor may take a while to show a state value…starts of with ‘unknown’

After about 2 hours of tinkering, learning, tinkering failing… Finally- success. Everything works perfect ! Thanks for your help- Greatly appreciated!

Rob

2 hours is not too bad when you say you are a newbee
And you have a learned a lot from the dark side LOL